<?php
/*
 * @version :  2013-3-18
 * @project :  account-system
 * @Author  :  Allen
 * @e-mail  :  shulei@maszy.cn
 * @des		:  用户注册
 */
/**
 * 调用配置文件，并连接数据库
 */
require_once (dirname(__FILE__)."/../../config/config.php");
if(empty($_SESSION['lang'])){
    $_SESSION['lang']= "cn";
}elseif($_GET['lang'] == "en"){
    $_SESSION['lang']= "en";
}elseif($_GET['lang'] == "cn"){
      $_SESSION['lang']= "cn";
}
$method = $_REQUEST['method'];
$userName = $_REQUEST['userName'];
if($method == 'valid'){
	$nub = validUserName($userName);
}

/**
 * 完成用户注册代码块
 */
if($method == 'add'){
	$errorTxt.='';
	$enErrorTxt.='';
	$successTxt.='';
	$valid = $_REQUEST['valid'];
	$userName = $_REQUEST['userName'];
	$password = $_REQUEST['password'];
	$repeatPassword = $_REQUEST['repeatPassword'];
	$level = $_REQUEST['level'];
	$email = $_REQUEST['userEmail'];
	$telephone = $_REQUEST['telephone'];
	if(!empty($password)){
		if(!empty($repeatPassword)){
			if(strlen($password) >= 6 && strlen($repeatPassword) >= 6){
				$number = strcmp($password,$repeatPassword);
				if(!$number == 0){
					$errorTxt.='两次密码输入不一致!';
					$enErrorTxt.='Two password input does not match!';
				}
			}else{
				$errorTxt.='密码的长度应大于等于6!';
				$enErrorTxt.='The length of the password can\'t less than 6!';
			}
		}else{
			$errorTxt.='重复密码不能为空!';
			$enErrorTxt.='Repeat password cannot be empty!';
		}
	}else{
		$errorTxt.='密码不能为空!';
		$enErrorTxt.='Password cannot be empty!';
	}
	if(empty($userName)){
		$errorTxt.='用户名不能为空!';
		$enErrorTxt.='User name cannot be empty!';
	}
	if(empty($email)){
		$errorTxt.='邮箱不能为空!';
		$enErrorTxt.='User name cannot be empty!';
	}else if(!validEmails($email)){
		$errorTxt.='邮箱输入不正确!';
		$enErrorTxt.='Email type is not correct!';
	}
	if(empty($telephone)){
		$errorTxt.='联系电话 /手机不能为空!';
		$enErrorTxt.='Contact phone/cell phone can\'t be empty!';
	}
	if($valid == 1){
		$errorTxt.='用户名已存在!';
		$enErrorTxt.='User name already exists!';
	}else if($valid == 2){
		$errorTxt.='请填写用户名!';
		$enErrorTxt.='Please fill in user name!';
	}else{
		if(empty($errorTxt)){
			$sql="insert into zy_user (`user_name`,`password`,`level`,`telephone`,`email`) value ('$userName','$password',$level,'$telephone','$email')";
			$rs = mysql_query($sql) or die(mysql_error());
			if($rs > 0){
				$successTxt.='恭喜您,用户注册成功!';
			}
		}
	}
}
/**
 * 验证用户是否存在
 */
function validUserName($userName){
	$sql="select * from zy_user where user_name='".$userName."' and  isdelete = 0";
	$rs = mysql_query($sql) or die(mysql_error());
	$row = mysql_fetch_array($rs);
	if($row){
		echo  '1';die();	//用户已经存在
	}else if(empty($userName)){
		echo '2';die();		//用户为空
	}else{
		echo '0';die();		//用户可以使用
	}
}

/**
 * 通过正则验证邮箱格式是否正确
 * @param unknown $address
 * @return boolean
 */
function validEmails($address)
{
	if(!preg_match("/^[0-9a-zA-Z_-]+(\.[0-9a-zA-Z_-]+)*@[a-zA-Z0-9_-]+(\.{1}[a-zA-Z0-9_-]+)*\.{1}[a-zA-Z]{2,4}$/i",$address))
	{
		return False;
	}
	return   True;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>NetCube</title>
  <meta name="viewport" content="width=device-width,initial-scale=1" />

    <link rel="stylesheet" type="text/css" href="style/reset.css" /> 
    <link rel="stylesheet" type="text/css" href="style/root.css" /> 
    
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/toogle.js"></script>
    <script type="text/javascript">
<!--
	function validUserName(){
	$userName = $("#userName").val();
	$.ajax({
		type: "POST",
		url: "register.php?method=valid&userName="+$userName,
		success:function(data){
		    $lang = $("#lang").val();
			if(data=="0"){
			    if($lang =='en'){
					$("#info").text("User name can be used!");
			    }else{
					$("#info").text("用户名可以使用!");
				}	
				$("#valid").attr("value",'0');
			}else if(data=="1"){
			    if($lang =='en'){
					$("#info").text("User name already exists, please change the user name!");
			    }else{
					$("#info").text("用户名已存在，请更换用户名!");
				}	
				$("#valid").attr("value",'1');
				$("#userName").focus();
			}else if(data=="2"){
			    if($lang =='en'){
			    	$("#info").text("Please fill in user name!");
			    }else{
				$("#info").text("请填写用户名!");
				}
				$("#valid").attr("value",'2');
				$("#userName").focus();
			}
		}
		});
}

/**
 * 验证密码长度
 */
function validPassLength(){
	$pass = $("#password").val();
	$lang = $("#lang").val();
	if($pass.length < 6){
		if($lang =='en'){
		    $("#infoPass").text("Password length should be greater than or equal to 6!");
		}else{
		    $("#infoPass").text("密码长度应大于或等于6!");
		}
		$("#userName").focus();
	}else{
	    if($lang =='en'){
	    	$("#infoPass").text("Password you can use!");
	    }else{
		 	$("#infoPass").text("密码可以使用!");
	    }
	}
}
/**
 * 验证重复密码长度
 */
function validRPassLength(){
	$pass = $("#repeatPassword").val();
	$lang = $("#lang").val();
	if($pass.length < 6){
	    if($lang =='en'){
			$("#infoRPass").text("Password length should be greater than or equal to 6!");
	    }else{
			$("#infoRPass").text("密码长度应大于或等于6!");
		}	
	    $("#repeatPassword").focus();
	}else{
	    if($lang =='en'){
			$("#infoRPass").text("Password you can use!");
	    }else{
			$("#infoRPass").text("密码可以使用!");
	    }
	}
}
/**
 * 验证输入的邮箱
 */
function validEmail(){
	$userEmail = $("#userEmail").val();
	$lang = $("#lang").val();
	var reg=/^[a-zA-Z]([a-zA-Z0-9]*[-_.]?[a-zA-Z0-9]+)+@([\w-]+\.)+[a-zA-Z]{2,}$/;
	if(!reg.test($userEmail)){
	    if($lang =='en'){
			$("#infoEmail").text("Please enter a valid Email address!");
	    }else{
			$("#infoEmail").text("请输入合法的Email地址!");
	    }
	    $("#userEmail").focus();
	}else{
	    if($lang =='en'){
	    	$("#infoEmail").text("Email address can use!");
	    }else{
			$("#infoEmail").text("Email地址可以使用!");
	    }
	}
}

/**
 * 验证输入的手机号码
 */
function validTelephone(){
	$telephone = $("#telephone").val();
	$lang = $("#lang").val();
	var reg=/(?:\(?[0\+]?\d{1,3}\)?)[\s-]?(?:0|\d{1,4})[\s-]?(?:(?:13\d{9})|(?:\d{7,8}))/;
	if(!reg.test($telephone)){
	    if($lang =='en'){
			$("#infoPhone").text("Please enter the legal cell phone number!");
	    }else{
			$("#infoPhone").text("请输入合法的手机号码!");
	    }
	    $("#telephone").focus();
	}else{
	    if($lang =='en'){
	    	$("#infoPhone").text("Phone number you can use!");
	    }else{
			$("#infoPhone").text("手机号码可以使用!");
	    }
	}
}
//-->
</script>
</head>
<body>


	
    <!-- start header -->
    	<div id="header">
         <a href="#"><img src="img/logo.png" width="77" height="24" alt="logo" class="logo" /></a>
         	<a href="index.php" class="button back"><img src="img/back-button.png" width="15" height="16" alt="icon" /></a>
        <div class="clear"></div>
        </div>
    <!-- end header --> 
    
    <!-- start page -->
    <div class="page">
    		
            <div class="simplebox">
            	<h1 class="titleh"><?php if($_SESSION['lang'] == 'en'){ echo "New Users Sign Up";}else{echo "新用户注册";};?></h1>
                <div class="content">
                	
                  <form id="form12" name="form1" method="post" action="register.php?method=add">
                  
                    <!--提示框-->
                    <?php if(!empty($errorTxt)){ ?>
                    <div class="albox errorbox" >
                      <b><?php  if($_SESSION['lang'] == 'en'){echo 'Input error:';}else{echo '输入错误：';}?></b><?php 
		                      if($_SESSION['lang'] == 'en'){
		                      	echo $enErrorTxt;
		                      }else{
		                      	echo $errorTxt;
		                      }
						}else{ ?>   
                       <div class="albox errorbox" style="display:none;">
                      <?php } ?>
                      <a href="#" class="close tips" title="close">close</a>
                    </div>
                     <?php if(!empty($successTxt)){ ?>
	                    <div class="albox succesbox">
	                            <?php 
				                  if($_SESSION['lang'] =='en'){
										echo '<b>Successful Operation:</b> Your password has been modifie,please <a href="http://cp.netcube.com.au">Click here</a> logging.';
									}else{
										echo '<b>操作成功 :</b> 您的密码已修改，请 <a href="http://cp.netcube.com.au">点击这里</a> 登录。';
									}
				                  ?>
	                           <a href="#" class="close tips" title="close">close</a>
	                     </div>
                  	<?php }?>
                    
                  		  <input type="hidden"  id="lang" name="lang" value="<?php echo $_SESSION['lang'];?>"/>
                    <div class="form-line">
                   	  <label class="st-label"><?php if($_SESSION['lang'] == 'en'){ echo "Username";}else{echo "用户名";};?><span id="info"  style="color:#BF2C11; font-weight:normal; margin-left:20px;"></span></label>
                      <input type="text"  id="userName" name="userName" onblur="validUserName();" style=" width:80%;"/>
                      <input type="hidden"  id="valid" name="valid"/>
                    </div>
                    
                    <div class="form-line">
                   	  <label class="st-label"><?php if($_SESSION['lang'] == 'en'){ echo "Password";}else{echo "密码";};?><span id="infoPass" style="color:#BF2C11; font-weight:normal; margin-left:20px;"></span></label>
                      <input type="text" id="password" name="password" onblur="validPassLength();"  style=" width:80%;"/>
                    </div>
                    
                    <div class="form-line">
                   	  <label class="st-label"><?php if($_SESSION['lang'] == 'en'){ echo "Verify Password";}else{echo "确定密码";};?><span id="infoRPass" style="color:#BF2C11; font-weight:normal; margin-left:20px;"></span></label>
                      <input type="text" id="repeatPassword" name="repeatPassword" onblur="validRPassLength();"  style=" width:80%;" />
                      <input type="hidden" class="Textbox" id="level" name="level" value="2"/>
                    </div>
                    
                    <div class="form-line">
                   	  <label class="st-label"><?php if($_SESSION['lang'] == 'en'){ echo "Email Address";}else{echo "电子邮箱";};?><span id="infoEmail" style="color:#BF2C11; font-weight:normal; margin-left:20px;"></span></label>
                       <input type="text" class="Textbox" id="userEmail" name="userEmail" onblur="validEmail();" style=" width:80%;" />
                    </div>
                    
                    <div class="form-line">
                   	  <label class="st-label"><?php if($_SESSION['lang'] == 'en'){ echo "Contact Phone/Phone";}else{echo "联系电话 / 手机";};?><span id="infoPhone" style="color:#BF2C11; font-weight:normal; margin-left:20px;"></span></label>
                      <input type="text" name="telephone" id="telephone"  style=" width:80%;" onblur="validTelephone();"/>
                    </div>
                    <!--  
                    <div class="form-line">
                   	  <label class="st-label">验证码</label>
                      <input type="text" name="textfield" id=""  style=" width:15%; float:left;" />
                      <img src="img/yanzhengma.jpg" style="float:left;" />
                      <a href="" style="margin: 25px 0 0 10px; display:block; float:left;">看不清？换一个</a>
                    </div>
                  	-->
                    <div class="clear"></div>
                  <!--  
                    <div class="form-line">
                      <label><input type="checkbox" name="checkbox" id="checkbox1" checked="checked" />我已阅读并同意<a href="#">《NetCube用户协议》</a></label>
                    </div>
                    -->  
                    <div class="form-line">
                    <input type="submit" name="button" id="button" value="<?php if($_SESSION['lang'] == 'en'){echo 'Submit';}else{echo '提交';}?>" class="submit-button" />
                    <input type="reset" name="button" id="button2" value="<?php if($_SESSION['lang'] == 'en'){echo 'Reset';}else{echo '重置';}?>" class="reset-button" />
                    </div>

                  </form>
                  
                </div>
      </div>
            
            
            
            
            
            
            <!-- start top button -->
            <div class="topbutton"><a href="#header"><span>Top</span></a></div>
            <!-- end top button -->
            
            
            
            <!-- start footer -->
            <div class="footer">
            © Copyright 2013 . developed by Netcube
            </div>
            <!-- end footer -->
            
            
    
    <div class="clear"></div>
    </div>
    <!-- end page -->
	
    
    
    
    
</body>
</html>
